package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;


    @Override
    public void queryPageRoom(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.queryPageRoom(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {

        //1.根据id 查询房间信息详情 RoomInfo
        RoomInfo roomInfo = roomInfoMapper.queryRoomById(id);
        if (roomInfo == null){
            return null;
        }
        //2.查询所属公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.queryApartmentById(roomInfo.getApartmentId());
        //3.查询图片列表 graphInfoList
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphById(ItemType.ROOM,id);
        //4.查询属性信息列表 attrValueList
        List<AttrValueVo> attrValueVoList = attrValueMapper.queryAttrById(id);
        //5.查询配套信息列表 facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.queryFacilityById(id);
        //6.查询标签信息列表 labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.queryLabelById(id);
        //7.查询支付方式列表 paymentTypeList
        List<PaymentType> paymentTypeList = paymentTypeMapper.queryListByRoomId(id);
        //8.查询可选租期列表 leaseTermList
        List<LeaseTerm> leaseTermList = leaseTermMapper.queryLeaseTermById(id);
        //9.查询费用项目信息 feeValueVoList
        List<FeeValueVo> feeValueVoList = feeValueMapper.queryFeeValueById(roomInfo.getApartmentId());
        //10.查询房间入住状态 LeaseAgreement
        LambdaQueryWrapper<LeaseAgreement> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LeaseAgreement::getRoomId,roomInfo.getId());
        lambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        Long count = leaseAgreementMapper.selectCount(lambdaQueryWrapper);
        //11.合并到 roomDetailVo 对象返回即可
        RoomDetailVo appRoomDetailVo = new RoomDetailVo();

        appRoomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        appRoomDetailVo.setIsCheckIn(count > 0);

        appRoomDetailVo.setApartmentItemVo(apartmentItemVo);
        appRoomDetailVo.setGraphInfoList(graphInfoList);
        appRoomDetailVo.setAttrValueVoList(attrValueVoList);
        appRoomDetailVo.setFacilityInfoList(facilityInfoList);
        appRoomDetailVo.setLabelInfoList(labelInfoList);
        appRoomDetailVo.setPaymentTypeList(paymentTypeList);
        appRoomDetailVo.setLeaseTermList(leaseTermList);
        appRoomDetailVo.setFeeValueVoList(feeValueVoList);

        //12.将原有对象的属性赋值给目标对象的相同属性！ 属性值迁移
        BeanUtils.copyProperties(roomInfo,appRoomDetailVo);
        return appRoomDetailVo;
    }

    @Override
    public void queryPageApartmentId(Page<RoomItemVo> page, Long id) {
        roomInfoMapper.queryPageApartmentId(page,id);
    }
}




